package com.aier.user.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.classes.entity.Classes;
import com.aier.common.common.CommonSession;
import com.aier.common.common.Constant;
import com.aier.common.common.ICommonDAO;
import com.aier.school.entity.School;
import com.aier.user.entity.Dynamic;
import com.aier.user.entity.Fresh;
import com.aier.user.entity.FreshComment;
import com.aier.user.entity.FreshPraise;
import com.aier.user.entity.Letter;
import com.aier.user.entity.LetterDetail;
import com.aier.user.entity.Notice;
import com.aier.user.entity.User;
import com.aier.user.service.IFindPeopleService;
import com.aier.user.service.IHomeForumsService;
import com.aier.user.service.IUserMessageService;
import com.aier.user.service.IUserService;

@Scope("prototype")
@Component("userMessageAction")
public class UserMessageAction extends UserBaseAction{

    /**
     * 通知,与我相关
     */
    private static final long serialVersionUID = 1L;
    
    @Resource(name="homeForumsService")
    private IHomeForumsService homeForumsService;
    @Resource(name="userMessageService")
    private IUserMessageService userMessageService;
    @Resource(name="userService")
    private IUserService userService;
    
    // 错误码
    private int s;
    // 页数
    private int page;
    // 每页的记录数
    private int page_size;
    // 总页数
    private int page_count;
    // 总记录数
    private int count;
    
    // 收到的评论
    private List<FreshComment> fcList;
    // 评论的id
    private Long fcid;
    
    // 对我的新鲜事的赞
    private List<FreshPraise> fpList;
    
    // 提到我的新鲜事
    private List<Fresh> freshList;
    
    // 收信人的用户id
    private Long ruid;
    // 收信人的用户id
    private Long suid;
    // 私信的内容
    private String content;
    // 上传的图片名称
    private String imageName;
    
    // 私信列表
    private List<Letter> letterList;
    
    // 私信id
    private Long lid;
    // 私信详情列表
    private List<LetterDetail> ldList;
    // 私信详情id(多个以，号分隔)
    private String ldidStr;
    // 未关注私信id (多个以，号分隔)
    private String lidStr;
    
    //通知列表
    private List<Notice> noticeList;
    
    // 与我相关
    private List<String> messageList;
    
    private List<Dynamic> dyList;
    
    private User userInfo;
    
   
    // 通知id
    private Long nid;
    
    private Long uid;
    
    // 发私信时输入的昵称
    private String nickname;
    
 

	@Resource(name="findPeopleService")
    private IFindPeopleService findPeopleService;
    
    private List<School> schoolList; // 用户所在学校
    private List<Classes> classList; // 用户所在班级
    
    @SuppressWarnings("unchecked")
    @Override
    public String execute(){
        schoolList = (List<School>) request.getSession().getAttribute(
                CommonSession.SESSION_ACCOUNT_SCHOOLLIST);
        classList = (List<Classes>) request.getSession().getAttribute(
                CommonSession.SESSION_ACCOUNT_CLASSLIST);
        // 查询个人资料
        userInfo = findPeopleService.searchSelfInfo((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID));
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 获取最新的消息
     * @return
     */
    public String searchNewMessage() {
        // 获取与我相关总列表
        messageList = new ArrayList<String>();
        // 获取最新的提到我的新鲜事
        messageList.add(userMessageService.searchSpecialNew((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID)));
        // 获取最新的评论
        messageList.add(userMessageService.searchCommentNew((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID)));
        // 获取最新的赞（对我的新鲜事）
        messageList.add(userMessageService.searchPraiseNew((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID)));
        // 获取最新的私信
        messageList.add(userMessageService.searchLetterNew((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID)));  
        // 获取最新的通知
        messageList.add(userMessageService.searchNoticeNew((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID))); 
        s = 1;
        return SUCCESS;
    }
    
    
    public String searchMessageAboutMe() {
    	dyList= new ArrayList<Dynamic>();
        // 获取最新的评论
    	List<Dynamic> list1=userMessageService.searchGetDynamicComment(uid);
        // 获取最新的                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   赞
        List<Dynamic> list2 =userMessageService.searchGetDynamicPraiseNew(uid);
        
        if(list1!=null){
        	dyList.addAll(list1);
        }
        if(list2!=null){
        	dyList.addAll(list2);
        }
        Collections.sort(dyList,new Comparator<Dynamic>(){
        	   public int compare(Dynamic d1, Dynamic d2) {
        		   return d1.getCdate().compareTo(d2.getCdate());	
            }
        });
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 获取收到的评论
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchReceiveComment() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String, Object> map = homeForumsService.searchReceiveComment(
                page, page_size, (Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID));
        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            fcList = (List<FreshComment>) map.get(ICommonDAO.OBJ_LIST);
        }
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 获取发出的评论
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchSendComment() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String, Object> map = homeForumsService.searchSendComment(
                page, page_size, (Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID));
        
        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            fcList = (List<FreshComment>) map.get(ICommonDAO.OBJ_LIST);
        }
        userInfo = findPeopleService.searchSelfInfo((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID));
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 删除收到的或发出的评论
     * @return
     */
    public String deleteComment() {
        s = userMessageService.deleteComment(fcid);
        return SUCCESS;
    }
    
    /**
     * 获取对我的微博的赞
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchPraiseMyFresh() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String, Object> map = homeForumsService.searchPraiseAllFresh(
                page, page_size, (Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID));
        
        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            fpList = (List<FreshPraise>) map.get(ICommonDAO.OBJ_LIST);
            if (fpList != null && fpList.size() != 0) {
                for (FreshPraise fp : fpList) {
                    fp.setFresh(homeForumsService.searchFresh(fp.getFid(),1));
                }
                
            }
                
        }
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 提到我的新鲜事
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchSpecialFresh() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String, Object> map = homeForumsService.searchSpecialFresh(
                page, page_size, (Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID));
        
        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            freshList = (List<Fresh>) map.get(ICommonDAO.OBJ_LIST);
            if (freshList != null && freshList.size() > 0) {
                for (Fresh fresh : freshList) {
                    if (fresh.getType() == 1) {
                        fresh.setFresh(homeForumsService.searchFresh(fresh.getFid(),1));
                    }
                        
                }
            }
                
        }
        uid = (Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID);
        
        // 新增或修改用户查询@我的新鲜事的记录
        userMessageService.insertAction((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID),(short)2);
        
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 提到我的评论
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchSpecialComment() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String, Object> map = homeForumsService.searchSpecialComment(
                page, page_size, (Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID));
        
        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            fcList = (List<FreshComment>) map.get(ICommonDAO.OBJ_LIST);
            
        }
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 发送对话形式私信
     * @return
     */
    public String sendLetter() {
        if (ruid == null) {
            // 根据昵称查找用户
            User user = userService.searchUserByNickname(nickname);
            if (user == null) {
                s = Constant.num_10001;
                return SUCCESS;
            }
            ruid = user.getUid();
        }
        
        s = userMessageService.insertLetter((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID), 
                ruid, (short)1, content, (short)0, imageName);
        return SUCCESS;
    }
    
    /**
     * 清空私信
     * @return
     */
    public String deleteLetter() {
        s = userMessageService.deleteLetter((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID));
        return SUCCESS;
    }
    
    /**
     * 获取私信列表
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchFollowLetter() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String, Object> map = userMessageService.searchFollowLetter(
                page, page_size, (Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID));
        
        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            letterList = (List<Letter>) map.get(ICommonDAO.OBJ_LIST);
        }
        // 新增或修改用户查询私信的记录
        userMessageService.insertAction((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID),(short)Constant.num_4);
        
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 查看私信详情
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchLetterDetail() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String, Object> map = userMessageService.searchLetterDetail(
                page, page_size, lid);
        
        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            ldList = (List<LetterDetail>) map.get(ICommonDAO.OBJ_LIST);
        }
        
        uid = (Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID);
        
        s = 1;
        
        return SUCCESS;
    }
    
    /**
     * 批量删除私信
     * @return
     */
    public String deleteLetterByLdid() {
        s = userMessageService.deleteLetterByLdid((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID), ldidStr);
        return SUCCESS;
    }
    
    /**
     * 查询通知列表
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchNoticeList() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String, Object> map = userMessageService.searchNoticeList(page, page_size, 
                (Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID));
        
        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            noticeList = (List<Notice>) map.get(ICommonDAO.OBJ_LIST);
        }
        // 新增或修改用户查询通知的记录
        userMessageService.insertAction((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID),(short)Constant.num_3);
        
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 删除一条通知
     * @return
     */
    public String deleteNotice() {
        s = userMessageService.deleteNotice(nid);
        return SUCCESS;
    }
    
    /**
     * 删除所有通知
     * @return
     */
    public String deleteAllNotice() {
        s = userMessageService.deleteAllNotice((Long) request.getSession().getAttribute(CommonSession.SESSION_ACCOUNT_ID));
        return SUCCESS;
    }

    public void setHomeForumsService(IHomeForumsService homeForumsService) {
        this.homeForumsService = homeForumsService;
    }

    public List<FreshComment> getFcList() {
        return fcList;
    }

    public List<FreshPraise> getFpList() {
        return fpList;
    }

    public List<Fresh> getFreshList() {
        return freshList;
    }

    public void setUserMessageService(IUserMessageService userMessageService) {
        this.userMessageService = userMessageService;
    }

    public void setRuid(Long ruid) {
        this.ruid = ruid;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public List<Letter> getLetterList() {
        return letterList;
    }

    public void setLid(Long lid) {
        this.lid = lid;
    }

    public List<LetterDetail> getLdList() {
        return ldList;
    }

    public void setLdidStr(String ldidStr) {
        this.ldidStr = ldidStr;
    }


    public void setLidStr(String lidStr) {
        this.lidStr = lidStr;
    }

    public int getS() {
        return s;
    }

    public List<Notice> getNoticeList() {
        return noticeList;
    }
    


    public List<String> getMessageList() {
        return messageList;
    }

    public User getUserInfo() {
        return userInfo;
    }

    public void setFindPeopleService(IFindPeopleService findPeopleService) {
        this.findPeopleService = findPeopleService;
    }

    public void setNid(Long nid) {
        this.nid = nid;
    }

    public void setImageName(String imageName) {
        this.imageName = imageName;
    }

    public int getCount() {
        return count;
    }

    public Long getUid() {
        return uid;
    }

    public void setFcid(Long fcid) {
        this.fcid = fcid;
    }

    public int getPage_count() {
        return page_count;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public Long getLid() {
        return lid;
    }

    public Long getRuid() {
        return ruid;
    }

    public Long getSuid() {
        return suid;
    }

    public void setSuid(Long suid) {
        this.suid = suid;
    }

    public List<School> getSchoolList() {
        return schoolList;
    }

    public List<Classes> getClassList() {
        return classList;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public void setPage_size(int page_size) {
        this.page_size = page_size;
    }

    public int getPage() {
        return page;
    }
    
    public List<Dynamic> getDyList() {
        return dyList;
    }
    
    public void setUid(Long uid) {
 		this.uid = uid;
 	}
}	
